package com.superm.controller;

import com.superm.entity.*;
import com.superm.service.GoodsService;
import com.superm.service.PurchaseService;
import com.superm.service.RepostoryService;
import com.superm.service.SupplierService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 采购申请单控制器
 */
@Controller
@RequestMapping("/purchase")
public class PurchaseController {



    @Autowired
    private SupplierService supplierService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private RepostoryService repostoryService;


    @Autowired
    private PurchaseService purchaseService;
    /**
     *跳转至添加采购申请单页面
     * @return
     */
    @RequestMapping("/skipAddPurchase")
    public ModelAndView skipAddPurchase(){
        ModelAndView mv = new ModelAndView("purchase/addpurchase");
        //查询所有供应商
        List<Supplier> supplierList  =   supplierService.allSup();
        //查询所有商品
        List<Goods> goodsList =  goodsService.selectGoodSs();
        //查询所有仓库
        List<RepostoryType> repostoryTypes = repostoryService.allRepostory();
        mv.addObject("repostoryTypes",repostoryTypes);
        mv.addObject("supplierList",supplierList);
        mv.addObject("goodsList",goodsList);
        return  mv;
    }


    /**
     * 采购神申请单添加
     * @return
     */
    @RequestMapping("/addPurchase")
    @ResponseBody
    public Map<String,Object> addPurchase(PurchasePoJo purchasePoJo, @RequestParam("purchases[]")List<String> purchaseDetails){
        System.out.println(purchaseDetails);
       Map<String,Object> map =  purchaseService.addPurchase(purchasePoJo,purchaseDetails);
        return  map;
    }


    /**
     * 跳转到商品采购页面
     * @return
     */
    @RequestMapping("/skipPurchase")
    public ModelAndView skipPurchase(){
        ModelAndView mv = new ModelAndView("purchase/purchaseShowList");
        //查询当前用户的部门
        //获取认证主体
        Subject subjet = SecurityUtils.getSubject();
        //从认证主体取出认证信息
        Emp emp = (Emp)subjet.getPrincipal();
        mv.addObject("emp",emp);
        return  mv;
    }


    /**
     * 模糊查询采购单
     * @return
     */
    @RequestMapping("/likePurchase")
    @ResponseBody
    public Map<String,Object> likePurchase(PurchasePoJo purchasePoJo,@RequestParam("page") Integer pageNo,@RequestParam("limit") Integer pageSize){
        Map<String,Object> map =  purchaseService.likePurshace(purchasePoJo,pageNo,pageSize);
        return map;
    }


    /**
     * 跳转到采购申请单详情
     * @return
     */
    @RequestMapping("/skipPurchaseDetails")
    public ModelAndView skipPurchaseDetails(@RequestParam("purCode") String purCode){
        ModelAndView mv = new ModelAndView("purchaseDetails/purchaseList");
        mv.addObject("purCode",purCode);
        return mv;
    }


    /**
     * 订单状态改变为经理已审批
     * @return
     */
    @RequestMapping("/purCodeState1")
    @ResponseBody
    public Map<String,Object> purCodeState1(@RequestParam("purCode") String purCode){
        Map<String,Object> map =   purchaseService.purchaseState1(purCode);
        return map;
    }


    /**
     * 财务打款审批
     * @param purCode
     * @return
     */
    @RequestMapping("/purCodeState2")
    @ResponseBody
    public Map<String,Object> purCodeState2(@RequestParam("purCode") String purCode){
        Map<String,Object> map =   purchaseService.purchaseState2(purCode);
        return map;
    }

    /**
     * 采购员采购
     * @param purCode
     * @return
     */
    @RequestMapping("/purCodeState3")
    @ResponseBody
    public Map<String,Object> purCodeState3(@RequestParam("purCode") String purCode){
        Map<String,Object> map =   purchaseService.purchaseState3(purCode);
        return map;
    }

    /**
     * 改变状态4
     * @param purCode
     * @return
     */
    @RequestMapping("/purCodeState4")
    @ResponseBody
    public Map<String,Object> purCodeState4(@RequestParam("purCode") String purCode){
        Map<String,Object> map =   purchaseService.purchaseState4(purCode);
        return map;
    }

    /**
     * 跳转至驳回填写备注页面(经理)
     * @return
     */
    @RequestMapping("/skiprejectCause")
    public ModelAndView skipRejectCause(@RequestParam("purCode") String purCode){
        ModelAndView mv = new ModelAndView("purchase/rejectCause");
        mv.addObject("purCode",purCode);
        return mv;
    }


    /**
     *跳转至驳回填写备注页面(财务)
     * @return
     */
    @RequestMapping("/skipRejectCauseCw")
    public ModelAndView skipRejectCauseCw(@RequestParam("purCode") String purCode){
        ModelAndView mv = new ModelAndView("purchase/rejectCauseCw");
        mv.addObject("purCode",purCode);
        return mv;
    }

    /**
     * 申请单驳回
     * @return
     */
    @RequestMapping("/purchaseReject")
    @ResponseBody
    public Map<String,Object> purchaseReject(@RequestParam("purCode") String purCode,@RequestParam("role") String role){
         Map<String,Object> map =  purchaseService.purchaseState5(purCode,role);
        return map;
    }


    /**
     * 跳转至填写出库备注页面
     * @return
     */
    @RequestMapping("/skipGodowEntry")
    public ModelAndView skipGodowEntry(@RequestParam("purCode") String purCode){
        ModelAndView mv = new ModelAndView("purchase/godowEntry");
        mv.addObject("purCode",purCode);
        return mv;
    }

    /**
     * 跳转至采购退货填写备注页面
     * @return
     */
    @RequestMapping("/skipOutBoundRemarks")
    public ModelAndView skipOutBoundRemarks(@RequestParam("purCode") String purCode){
        ModelAndView mv = new ModelAndView("purchase/outboundRemarks");
        mv.addObject("purCode",purCode);
        return  mv;
    }

    /**
     * 采购单退货出库
     * @return
     */
    @RequestMapping("/purchaseOutBound")
    @ResponseBody
    public Map<String,Object> purchaseOutBound(@RequestParam("purCode") String purCode,@RequestParam("remarkes") String remarkes){
       Map<String,Object> map = purchaseService.purchaseOutBound(purCode,remarkes);
        return map;
    }

    /**
     * 采购单入库
     * @return
     */
    @RequestMapping("/goodsWarehousing")
    @ResponseBody
    public Map<String,Object> goodsWarehousing(@RequestParam("purCode") String purCode,@RequestParam("remarkes") String remarkes){
       Map<String,Object> map =  purchaseService.goodsWraehousing(purCode,remarkes);
        return map;
    }


    /**
     * 跳转至采购退货页面
     * @return
     */
    @RequestMapping("/skipdeletePurchase")
    public ModelAndView skipdeletePurchase(){
        ModelAndView mv = new ModelAndView("purchase/deletePurchase");
        //查询所有供应商
        List<Supplier> supplierList  =   supplierService.allSup();
        //查询所有仓库
        List<RepostoryType> repostoryTypes = repostoryService.allRepostory();
        mv.addObject("repostoryTypes",repostoryTypes);
        mv.addObject("supplierList",supplierList);
        return  mv;
    }
}
